<?php
/*
 * Copyright 2013 by Sawyer Bowman, Jim Garvey, Kevin Tabb, Nick Wetzel, and Allen
 * Tucker.  This program is part of Homeplate, which is free software.  It comes
 * with absolutely no warranty.  You can redistribute and/or modify it under the
 * terms of the GNU Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/).
 */

/**
 *  This file implements the viewing list of all donations in a given day.
 *  
 *  @author Allen Tucker, Sawyer Bowman & Nick Wetzel (editDonationLog form was replaced)
 *  @version December 21, 2013
 */
 
	session_start();
	session_cache_expire(30);
	
	// Setting the default time zone
	date_default_timezone_set('America/New_York');
?>
<html>
	<head>
		<title>
			Daily Donation Log
		</title>
		<link rel="stylesheet" href="styles.css" type="text/css" />
	</head>
	<body>
		<div id="container">
			<?php include('header.php');?>
			<div id="content">
				<?php 
				include_once('database/dbDonationLogs.php');
				include_once('domain/DonationLog.php');

				$thisDay = $_GET['date'];
				$today = date('y-m-d');	
				$todayUTC = time();
				$thisUTC = mktime(0,0,0,substr($thisDay,3,2),substr($thisDay,6,2),substr($thisDay,0,2));
				$nextdayUTC = $thisUTC + 86400;
				$prevdayUTC = $thisUTC - 86400;
				echo "<p style='font-size:16px'><b>Daily Donation Log for ".date('l F j, Y', $thisUTC)."</b></p>";
				echo "<hr>";
				?>
<table>
<tr>
<?php
	echo "<td class='padded'><a href=donationLogView.php?date=".date('y-m-d',$prevdayUTC)."><< Previous Day</a></td>";
	echo "<td></td>";
	echo "<td class='padded'><a href=donationLogView.php?date=".date('y-m-d',$nextdayUTC).">Next Day >></a></td>";	
?>
</tr>
	<tr>
		<td class="padded"><b>Time</b></td>
		<td class="padded" style='width:250px'><b>Donor *</b></td>
		<td class="padded"><b>Weight</b></td>	
		<?php
		// if we are one the current day, add the ability to add a donation
		if($today == $thisDay){
			echo("<td class='padded'>");
			echo("<button type='submit' form='addDonation'><b>Add Donation</b></button>");
		}
		?>		
	</tr>
	
	<?php
	$todaysDonationLog = get_donationLog($thisDay);
	if (!$todaysDonationLog) { // if there isn't one, make one
		$todaysDonationLog = new DonationLog($thisDay,"","","");
		insert_dbDonationLogs($todaysDonationLog);
	}			
	// first check if the log is empty
	if($todaysDonationLog->get_num_donations()){
		// if not empty then display each donation
		foreach ($todaysDonationLog->get_donations() as $aDonationID){
		
			$aDonation = retrieve_dbDonations($aDonationID);
			echo '<tr><td class="padded">'.$aDonation->get_pretty_time().'</td>';		
			echo '<td class="padded"><a href="viewDonation2.php?donationID='.$aDonation->get_id().'">'.$aDonation->get_donor_id().'</a></td>';
			echo '<td class="padded">'.$aDonation->get_total_weight().'</td></tr>';
		}
	}
	?>
	
</table>	
<p><b>* </b>View, edit or remove a donation by clicking its Donor.</p>

<form name="addDonation" id="addDonation" action="viewDonation2.php" method="get">
	<input type="hidden" name="donationID" id="donationID" value="new">
</form>
</div>
			
<?php include('footer.inc');?>
</div>
</body>
</html>